#include "stdafx.h"
#include "StringUtil.h"

#include <random>

USING_NS_UTILS

std::string StringUtil::toLower(const std::string& str)
{
	std::string result;
	for (int i = 0; i < str.length(); i++)
	{
		result.push_back(tolower(str[i]));
	}
	return result;
}

std::string StringUtil::toUpper(const std::string& str)
{
	std::string result;
	for (int i = 0; i < str.length(); i++)
	{
		result.push_back(toupper(str[i]));
	}
	return result;
}

int StringUtil::toInt(const std::string& str, int defaultValue)
{
	if (str.empty())
	{
		return defaultValue;
	}
	return atoi(str.c_str());
}

long StringUtil::toLong(const std::string& str, long defaultValue)
{
	if (str.empty())
	{
		return defaultValue;
	}
	return atol(str.c_str());
}

long long StringUtil::toLongLong(const std::string& str, long long defaultValue)
{
	if (str.empty())
	{
		return defaultValue;
	}
	return atoll(str.c_str());
}

double StringUtil::toDouble(const std::string& str, double defaultValue)
{
	if (str.empty())
	{
		return defaultValue;
	}
	return atof(str.c_str());
}

std::string StringUtil::random(int length)
{
	static std::string seed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	static std::random_device rd;
	static std::default_random_engine generator(rd());
	std::string result;
	for (int i = 0; i < length; i++)
	{
		result.push_back(seed[generator() % seed.length()]);
	}
	return result;
}

std::vector<std::string> StringUtil::split(const std::string& str, const std::string& pattern)
{
	std::vector<std::string> result;
	size_t pos, bpos = 0;
	while ((pos = str.find(pattern, bpos)) != std::string::npos)
	{
		result.push_back(str.substr(bpos, pos - bpos));
		bpos = pos + pattern.length();
	}
	result.push_back(str.substr(bpos));
	return result;
}

std::string StringUtil::join(const std::vector<std::string>& strs, const std::string& pattern)
{
	std::string result;
	for (int i = 0; i < strs.size(); i++)
	{
		result.append(strs[i]);
		result.append(pattern);
	}
	return result.substr(0, result.length() - pattern.length());
}

std::string StringUtil::trimmed(const std::string& str)
{
	std::string result = str;
	result.erase(str.find_last_not_of(" \r\n\t") + 1);
	result.erase(0, str.find_first_not_of(" \r\n\t"));
	return result;
}

std::string StringUtil::replace(const std::string& str, const std::string& originalString, const std::string& replaceString)
{
	std::string result = str;
	size_t pos = 0;
	while ((pos = result.find(originalString, pos)) != std::string::npos)
	{
		result.replace(pos, originalString.length(), replaceString);
		pos += replaceString.length();
	}
	return result;
}

char* StringUtil::U2G(const char* utf8)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}

char* StringUtil::G2U(const char* gb2312)
{
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}
